<?php
    session_start();
    require_once 'php/database.php';
    function curPageURL() {
         $pageURL = 'http';
         if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
         $pageURL .= "://";
         if ($_SERVER["SERVER_PORT"] != "80") {
          $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
         } else {
          $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
         }
         return $pageURL;
    }

    function getUserNamePage(){
        if (isset($_SESSION['username']))
	{
		$login = true;
	}
	else{
		$login = false;
		//header("Location: ".$CFG->url."home.php");
	}
	$loguname = $_SESSION['username'];
	$username = $_GET["un"];
	if ($username==NULL&&$login){
		$username=$loguname;
	}
        return $username;
    }


    function getIdThread(){
        $id = getIdThread2();
        if ($id != NULL)
	{
		$ada = true;
	}
	else{
		$ada = false;
	}
	$idthread = $_GET["id"];
	if ($idthread==NULL&&$ada){
		$idthread=$id;
	}
        return $idthread;
    }

    function getIdThread2(){
        $url = curPageURL();
        $k = -1;
        $sleng = strlen($url);
        for ($i = 0; $i < $sleng; ++$i){
            if ($url[$i] == '&'){
                $k = $i + 1;
            }
        }
        if ($k != -1){
            return substr($url, $k);
        }
        else{
            return "";
        }

    }

    $idthreadsaatini = getIdThread();
    //$user = $_SESSION['username'];
    $user = getUserNamePage();


    database::query("INSERT INTO message (Pengirim, Pesan, IDThread, TanggalKirim)
        VALUES ('".$user."', '".$_POST['pesanarea']."', '".$idthreadsaatini."', CURRENT_TIMESTAMP )");


    header("Location: messagedetail.php?un=$user&id=$idthreadsaatini");
?>


